package com.globallogic.vkulyk.BinaryTree;

public class Node {

	private String value = "";
	private Node leftNode = null;
	private Node rightNode = null;

	public Node(String value) {
		this.value = value;
	}

	public void add(String newValue) {
		if (newValue.compareTo(value) < 0) {
			if (leftNode == null) {
				leftNode = new Node(newValue);
			} else {
				leftNode.add(newValue);
			}
		} else if (newValue.compareTo(value) > 0) {
			if (rightNode == null) {
				rightNode = new Node(newValue);
			} else {
				rightNode.add(newValue);
			}
		}
	}

	@Override
	public String toString() {
		String result = "";
		if (leftNode != null) {
			result = leftNode.toString();
		}
		result = result + " " + value;
		if (rightNode != null) {
			result = result + rightNode.toString();
		}
		return result;
	}

	public int getHeight() {
		int depth = 1;
		int lDepth = 0;
		int rDepth = 0;
		if (leftNode != null) {
			depth += leftNode.getHeight();
		} else if (rightNode != null) {
			depth += rightNode.getHeight();
		}
		depth += Math.max(lDepth, rDepth);
		return depth;
	}

}
